package at.jaki.ubb.crawling.demo.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Utils {
	private Utils() {
	}

	/**
	 * Will change a string of form ab12[00-99]ab to {ab1200ab,...,ab1299ab}
	 * 
	 * @return List of string
	 */
	public static List<String> constructList(String s) {
		try {
			int i1 = s.indexOf('[');
			int i2 = s.indexOf(']');
			String s1 = s.substring(0, i1);
			String s2 = s.substring(i1 + 1, i2);
			String s3 = s.substring(i2 + 1, s.length());
			String[] s2b = s2.split("[-]");
			int size = s2b[0].length();
			int e1 = Integer.parseInt(s2b[0].trim());
			int e2 = Integer.parseInt(s2b[1].trim());
			List<String> list = new ArrayList<String>(e2 - e1 + 1);
			for (int i = e1; i <= e2; i++) {
				list.add(s1 + pad(i, size) + s3);
			}
			return list;
		} catch (Exception e) {
			return Collections.singletonList(s);
		}
	}

	private static String pad(Integer i, int size) {
		StringBuilder st = new StringBuilder(i.toString());
		while (st.length() < size) {
			st.insert(0, '0');
		}
		return st.toString();
	}

	public static void main(String[] args) {
		System.out.println(constructList("abc[000-010]abc"));
		System.out.println(constructList("[000-010]abc"));
		System.out.println(constructList("abc[000-010]"));
	}

}
